package com.ruge.sorm.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Properties工具类
 *
 * @author admin
 * @version 2023/10/21 10:45
 **/
public class PropertiesUtils {
    private static Properties prop = new Properties();
    private static Map<String, String> PROPER_MAP = new ConcurrentHashMap<>();

    static {
        InputStream is = null;
        try {
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties");
            assert is != null;
            prop.load(new InputStreamReader(is, StandardCharsets.UTF_8));

            for (Object o : prop.keySet()) {
                String key = (String) o;
                PROPER_MAP.put(key, prop.getProperty(key));
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getString(String key) {
        return PROPER_MAP.get(key);
    }

    public static void main(String[] args) {
        System.out.println(getString("spring.datasource.url"));
    }
}
